package com.whut.whw.personalinfo.mythings;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.whut.whw.R;
import com.whut.whw.model.Goods;
import com.whut.whw.util.EmptyUtils;
import com.whut.whw.util.TimeUtils;

import java.util.List;

import static com.whut.whw.constant.ConfigConsts.IMG_URL;

/**
 * <pre>
 *  desc:用于展示RecyclerView中信息，以及注册监听事件
 *  Created by 忘尘无憾 on 2017/06/23.
 *  version:
 * </pre>
 */
public class MyThingsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Goods> informModelList;//我的物品基本数据集
    private Context context;
    private static final int TYPE_ITEM = 0;//列表项
    private static final int TYPE_FOOTER = 1;//头
    private OnItemClickListener onItemClickListener;
    private int flag;
    private boolean footerFlag = false;

    public void setFooterFlag(boolean footerFlag) {
        this.footerFlag = footerFlag;
    }

    public boolean isFooterFlag() {
        return footerFlag;
    }

    /**
     * 一个点击事件接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);//每项的点击事件

        void onBtn1Click(View view, int position);//第一个按钮的点击

        void onBtn2Click(View view, int position);//第二个按钮的点击

        void onMoreClick(View view, int position);//更多操作的点击
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 构造方法
     * 其中flag用于标识是已发布还是已完结
     *
     * @param context
     * @param informModelList
     * @param flag
     */
    public MyThingsRecyclerViewAdapter(Context context, List<Goods> informModelList, int flag) {
        this.informModelList = informModelList;
        this.context = context;
        this.flag = flag;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {//如果是列表项
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_my_things_item, parent, false);
            return new MyThingsHolder(view);
        } else if (viewType == TYPE_FOOTER) {//如果是尾部
            View view = LayoutInflater.from(context).inflate(R.layout.default_loading, parent, false);
            return new FootHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof MyThingsHolder) {

            Goods informModel = informModelList.get(position);//获取每项的数据
            MyThingsHolder myThingsHolder = (MyThingsHolder) holder;//强转为MyThingsHolder类型

            if (EmptyUtils.isNotEmpty(informModel.getZp())) {
                Glide
                        .with(context)
                        .load(IMG_URL + (informModel.getZp().split(";"))[0])
                        .into(myThingsHolder.mythings_img_iv);//设置图片
            } else {
                Glide
                        .with(context)
                        .load(R.drawable.logo)
                        .into(myThingsHolder.mythings_img_iv);//设置图片
            }

            myThingsHolder.mythings_title_tv.setText(informModel.getBt());//设置标题
            myThingsHolder.mythings_likes_tv.setText(String.valueOf(informModel.getDzsl()));//设置点赞量
            myThingsHolder.mythings_comment_tv.setText(String.valueOf(informModel.getPlsl()));//设置评论量
            myThingsHolder.mythings_time_tv.setText(TimeUtils.date2String(informModel.getFbsj()));//设置时间
            if (flag == 0) {//已发布，设置第一个按钮为有兴趣，第二个为使完结
                myThingsHolder.mythings_btn1.setText(R.string.mythings_wants);
                myThingsHolder.mythings_btn2.setText(R.string.mythings_finish);
            } else if (flag == 1) {//已完结，隐藏第一个按钮，使第二个为取消完结
                myThingsHolder.mythings_btn1.setVisibility(View.INVISIBLE);
                myThingsHolder.mythings_btn2.setText(R.string.mythings_finish_back);
            }

            setListener(myThingsHolder);//设置监听事件
        } else if (holder instanceof FootHolder) {
            FootHolder footHolder = (FootHolder) holder;
            if (footerFlag) {
                footHolder.loading_liearlayout.setVisibility(View.GONE);
                footHolder.loading_finished_linearlayout.setVisibility(View.VISIBLE);
            } else {
                footHolder.loading_liearlayout.setVisibility(View.VISIBLE);
                footHolder.loading_finished_linearlayout.setVisibility(View.GONE);
            }
        }
    }

    private void setListener(final MyThingsHolder myThingsHolder) {
        //每项的点击事件
        myThingsHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = myThingsHolder.getLayoutPosition();
                onItemClickListener.onItemClick(myThingsHolder.itemView, position);
            }
        });

        //第一个按钮的点击事件
        myThingsHolder.mythings_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = myThingsHolder.getLayoutPosition();
                onItemClickListener.onBtn1Click(myThingsHolder.mythings_btn1, position);
            }
        });

        //第二个按钮的点击事件
        myThingsHolder.mythings_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = myThingsHolder.getLayoutPosition();
                onItemClickListener.onBtn2Click(myThingsHolder.mythings_btn2, position);
            }
        });

        //更多操作的点击事件
        myThingsHolder.mythings_more_imgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = myThingsHolder.getLayoutPosition();
                onItemClickListener.onMoreClick(myThingsHolder.mythings_more_imgbtn, position);
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        //判断是否是最后一个，因为position是从0开始所以+1
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        /**
         * 判断informList是否为空
         * 如果不为空，则在该长度上多加一行，可用于上拉加载
         */
        return EmptyUtils.isEmpty(informModelList.size()) ? 0 : informModelList.size() + 1;
    }


    class MyThingsHolder extends RecyclerView.ViewHolder {

        private ImageView mythings_img_iv;
        private TextView mythings_title_tv;
        private TextView mythings_likes_tv;
        private TextView mythings_comment_tv;
        private TextView mythings_time_tv;
        private Button mythings_btn1;
        private Button mythings_btn2;
        private ImageButton mythings_more_imgbtn;

        public MyThingsHolder(View itemView) {
            super(itemView);
            mythings_img_iv = (ImageView) itemView.findViewById(R.id.mythings_img_iv);
            mythings_title_tv = (TextView) itemView.findViewById(R.id.mythings_title_tv);
            mythings_likes_tv = (TextView) itemView.findViewById(R.id.mythings_likes_tv);
            mythings_comment_tv = (TextView) itemView.findViewById(R.id.mythings_comment_tv);
            mythings_time_tv = (TextView) itemView.findViewById(R.id.mythings_time_tv);
            mythings_btn1 = (Button) itemView.findViewById(R.id.mythings_btn1);
            mythings_btn2 = (Button) itemView.findViewById(R.id.mythings_btn2);
            mythings_more_imgbtn = (ImageButton) itemView.findViewById(R.id.mythings_more_imgbtn);
        }
    }

    /**
     * <pre>
     *  @Decription 底部的正在加载
     *  @datetime 2017/06/15 9:29
     *  @author 忘尘无憾
     *  @version
     * </pre>
     */
    static class FootHolder extends RecyclerView.ViewHolder {

        LinearLayout loading_finished_linearlayout;
        LinearLayout loading_liearlayout;

        public FootHolder(View itemView) {
            super(itemView);
            loading_finished_linearlayout = (LinearLayout) itemView.findViewById(R.id.loading_finished_linearlayout);
            loading_liearlayout = (LinearLayout) itemView.findViewById(R.id.loading_linearlayout);
        }
    }
}
